/*
* ==============================================================================
*  Name        : genericview.h
*  Interface   :
*  Description :
*  Version     :
*
 Copyright (c) 2005-2006 Jose Thachil
* ==============================================================================
*/

#ifndef __GENERICVIEW_H__
#define __GENERICVIEW_H__

#include <aknview.h>
#include <aknutils.h>
#include <AknViewAppUi.h> 
#include <aknappui.h>
//#include "ImageViewerAppUi.h"

/**
 * CGenericView is a template class for creating a view for Avkon 
 * view-switching architecture.
 *
 * @param T container class used by this view
 */

template <class T>
class CGenericView : public CAknView
    {
    public: // New methods

        static CGenericView* NewL( TInt aViewResource,TInt aMenubarResource, TUid aViewId);
        static CGenericView* NewLC( TInt aViewResource,TInt aMenubarResource, TUid aViewId);

        /**
        * Virtual Destructor.
        */
        virtual ~CGenericView();

    public: // Functions from base classes from CAknView

        TUid Id() const;
        void HandleCommandL( TInt aCommand );
        void DoActivateL( const TVwsViewId& aPrevViewId,TUid aCustomMessageId,const TDesC8& aCustomMessage);
		void DoDeactivate();
        void DynInitMenuPaneL( TInt aResourceId, CEikMenuPane* aMenuPane );

    public:
        void HandleSizeChange( TInt aType );
        void MenuItemToDimm( TInt aMenuItemId );
        
        T* GetContainer();
 
private: 
        void ConstructL( TInt aViewResource );
        CGenericView( TUid aViewId, TInt aMenubarResource );
 
        
private: 
        T*   iContainer;
        TUid iIdentifier;
        TInt iMenubarResource;
        TInt iItemToDimm;
    };
    


template <class T> CGenericView<T>::CGenericView( TUid aViewId,TInt aMenubarResource ) : 
												  iIdentifier( aViewId ),iMenubarResource( aMenubarResource ),iItemToDimm(0)
    {        
    }


template <class T> CGenericView<T>::~CGenericView()
    {
    if(iContainer) 
		{
		AppUi()->RemoveFromStack(iContainer);
		delete iContainer;
		}
    }
  
template <class T> CGenericView<T>* CGenericView<T>::NewL(TInt aViewResource, TInt aMenubarResource, TUid aViewId )
    {
    CGenericView<T>* self = CGenericView<T>::NewLC( aViewResource,aMenubarResource, aViewId);
    CleanupStack::Pop( self );
    return self;    
    }

template <class T> CGenericView<T>* CGenericView<T>::NewLC(TInt aViewResource, TInt aMenubarResource, TUid aViewId )
    {
    CGenericView<T>* self = new ( ELeave ) CGenericView<T>( aViewId,aMenubarResource);
    CleanupStack::PushL( self );
    self->ConstructL( aViewResource );
    return self;    
    }
    
template <class T> void CGenericView<T>::ConstructL( TInt aViewResource )
    {
    BaseConstructL( aViewResource );        
    }

template <class T> TUid CGenericView<T>::Id() const
    {
    return iIdentifier;
    }

template <class T> void CGenericView<T>::DoActivateL(const TVwsViewId& /*aPrevViewId*/,TUid /*aCustomMessageId*/,
										             const TDesC8& /*aCustomMessage*/)
    {
    if (!iContainer)
        {
		iContainer = new ( ELeave ) T();
		iContainer->SetMopParent( this );
		iContainer->ConstructL(ClientRect());
       
    if( iMenubarResource > 0 )
        {
        CEikMenuBar* menuBar = MenuBar();
        menuBar->SetContextMenuTitleResourceId( iMenubarResource ); 
        }
        
      AppUi()->AddToStackL(*this,iContainer);
      }
    
    //iContainer->UpdateContainer();
    iContainer->MakeVisible( ETrue );
      
    }

// -----------------------------------------------------------------------------
// CGenericView::DoDeactivate()
// DeActivate view
// -----------------------------------------------------------------------------
//
template <class T> void CGenericView<T>::DoDeactivate()
    {
    if ( iContainer )
        {
        // make the view invisible
        iContainer->MakeVisible( EFalse );
        }
    }

// -----------------------------------------------------------------------------
// CGenericView::HandleCommandL()
// Takes care of Command handling.
// -----------------------------------------------------------------------------
//
template <class T> void CGenericView<T>::HandleCommandL( TInt aCommand )
   {
   switch( aCommand )
           {
           case 44:
       	//	iContainer->RemoveItemL();
       	    break;    	
       	default:    
               AppUi()->HandleCommandL( aCommand );
              
           }
   }


template <class T> void CGenericView<T>::HandleSizeChange( TInt aType )
    {
    if( iContainer )
        {
        iContainer->HandleResourceChange( aType );
        
        if( aType==KEikDynamicLayoutVariantSwitch )
            {        
            TRect rect;
            AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
            iContainer->SetRect(rect);
            }
        }         
    }
 
// ---------------------------------------------------------------------------
// CGenericView::MenuItemToDimm()
// With this function is possible to dimm selected menuitem from options 
// menu or from its submenus.
// ---------------------------------------------------------------------------
//
template <class T> void CGenericView<T>::MenuItemToDimm( TInt aMenuItemId )
    {
    iItemToDimm = aMenuItemId;    
    } 
 
// ---------------------------------------------------------------------------
// CGenericView::DynInitMenuPaneL()
// This function is called by the EIKON framework just before it displays a 
// menu pane.
// ---------------------------------------------------------------------------
//
template <class T> void CGenericView<T>::DynInitMenuPaneL( TInt /*aResourceId*/,CEikMenuPane* aMenuPane )
    {            
    }  
template <class T> T* CGenericView<T>::GetContainer()
	{
	return iContainer;
	}
    

#endif // __GENERICVIEW_H__
